<?php
namespace app\member\controller;
use think\Controller;
use PHPMailer\PHPMailer\PHPMailer;
use think\api\Client;

Class Account extends Controller
{
    //登录
    public function login(){
    	if(request()->isPost()){
    		$data=input('post.');
    		return model('TpUser')->login($data);
    	}
        return view();

    }



    //检查用户是否登录(头部信息)
    public function checkLogin(){
        if(session('uid')){
            $arr=array('error'=>0,'uid'=>session('uid'),'username'=>session('username'));
            return json($arr);

        }else{
            //如果没有登录，检查cookie，如果有cookie恢复登录信息
            if(cookie('username') && cookie('password')){
                $data['username']=encryption(cookie('username'),1);
                $data['password']=encryption(cookie('password'),1);//cookie解密
                $loginRes=model('TpUser')->login($data);//(放回的$loginRes是json对象,php如何把json对象转化维数组？？？)
                // $loginRes=json_decode($loginRes);   
                if($loginRes['error']==0){
                    $arr=array('error'=>0,'uid'=>session('uid'),'username'=>session('username'));
                    return json($arr);
                }
            }
            $arr=array('error'=>1);
            return json($arr);
        }       
    }



    //注册
    public function reg(){
    	if(request()->isPost()){
    		$data=input('post.');

    		//验证
    		$validate = new \app\member\validate\User;
        	if (!$validate->check($data)) {
        		$this->error($validate->getError());
        	}
    		$userData=array();
    		$userData['username']=trim($data['username']);
    		$userData['password']=md5($data['password']);
    		$userData['mobile_phone']=$data['mobile_phone'];
    		$userData['email']=$data['email'];
    		$userData['register_type']=$data['register_type'];
    		$userData['register_time']=time();
           
    		// dump($userData);die;
    		$add=db('tp_user')->insert($userData);
    		if($add){
    			$this->success('注册成功,请登录','member/Account/login');
    		}else{
    			$this->error('注册失败');
    		}
	
    	}
        return view();

    }

    //找回密码
    public function getPassword(){
        return view();
    }


    //通过邮箱找回密码
     public function getPasswords(){
        if(request()->isAjax()){
            $data=input('post.');
            $dataArr=$data['data'];
            // return $dataArr['username'];
            $res=db('tp_user')->where(array('username'=>$dataArr['username'],'email'=>$dataArr['email']))->find();
            if($res){
                if($dataArr['send_code'] == session('emailPassword')){

                    $password=mt_rand(100000,999999);
                    db('tp_user')->where(array('username'=>$dataArr['username'],'email'=>$dataArr['email']))->update(['password'=>md5($password)]);
                    $sendRes=sendemail($dataArr['email'],$password);
                    if($sendRes){
                        $arr=array('error'=>0,'msg'=>'密码已发到邮箱，注意查收');
                        return json($arr);

                    }
                    
                }else{
                    $arr=array('error'=>1,'msg'=>'<i class="iconfont icon-minus-sign"></i>验证码错误');
                    return json($arr);

                }

                }else{
                    $arr=array('error'=>2,'msg'=>'<i class="iconfont icon-minus-sign"></i>不存在该用户');
                    return json($arr);
                }
           
        }
    }



   //找回密码发送邮件
    public function email_password($email){
        //判断是否存在该邮箱
        $res=db('tp_user')->where(['email'=>$email])->find();
        
        if($res){
            $sendRes=sendemail($email);
            
            if($sendRes){
                session('emailPassword',session('code'));

                //记录邮件验证码
                $msg=['status'=>0,'msg'=>'发送成功'];
                return json($msg);
            }else{
                $msg=['status'=>1,'msg'=>'发送失败'];
                return json($msg);
            }
        }else{
            $msg=['status'=>2,'msg'=>'<i class="iconfont icon-minus-sign"></i>不存在该邮箱'];
            return json($msg);
        }

    }

    public function sendPhoneCode(){
        $client = new Client("7d2831fedf9765f28cd26ea6c30ecce4");
        $code=rand(111111,999999);
        $phone=input('phone');
        $result = $client->smsSend()
            ->withSignId('116')
            ->withTemplateId('4')
            ->withPhone($phone)
            ->withParams('{"code":'.$code.'}')
            ->request();
        if($result['code']==0){
            session('phoneCode',$code);
        }
        return json($result);
    }



    //注册时发送邮件（user_register.js）
    public function sendmail(){
        $email=input('email');

       $sendRes=sendemail($email);
           // dump($sendRes);die;
        if($sendRes){
            session('emailCode',session('code'));
        	
            //记录邮件验证码
            $msg=['status'=>0,'msg'=>'发送成功'];
            return json($msg);
        }else{
            $msg=['status'=>1,'msg'=>'发送失败'];
            return json($msg);
        }

    }



  //判断用户名是否注册了
    public function is_registered(){
    	if(request()->isAjax()){
    		$search=db('tp_user')->where(array('username'=>input('username')))->find();
    		return true;
    		if($search){
    			return false;
    		}else{
    			return true;
    		}
    	}else{
    		$this->error('非法请求');
    	}
    }



    //判断邮箱是否被注册了
    public function check_email(){
    	if(request()->isAjax()){
    		$search=db('tp_user')->where(array('email'=>input('emai')))->find();
    		return true;
    		if($search){
    			return false;
    		}else{
    			return true;
    		}
    	}else{
    		$this->error('非法请求');
    	}

    }



    //判断手机是否被注册
    public function check_phone(){
    	if(request()->isAjax()){
    		$search=db('tp_user')->where(array('mobile_phone'=>input('mobile_phone')))->find();
    		return true;
    		if($search){
    			return false;
    		}else{
    			return true;
    		}
    	}else{
    		$this->error('非法请求');
    	}
    }



    //异步验证注册邮箱验证码
    public function checkd_email_code(){
   	if(request()->isAjax()){

   		if(session('emaiCode'==input('send_code'))){
   			return true;
   		}else{
   			return flase;
   		}
   	}else{
   		$this->error('非法请求');
   	}
        return true;
    }


    //异步验证手机验证码
    public function checkd_mobile_code(){
   	if(request()->isAjax()){
           if(session('phoneCode'==input('mobile_code'))){
               return true;
           }else{
               return flase;
           }

   	}else{
   		$this->error('非法请求');
   	}
        
    }












}